package com.ibeeking.found.cop.b.rest.service.mq;

import com.ibeeking.found.common.constants.MqTopic;
import com.ibeeking.found.cop.service.common.enums.MqSyncOperatorTypeEnum;
import com.ibeeking.found.cop.service.common.param.MqSyncCopParam;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.rocketmq.annotation.MQConsumer;
import com.ibeeking.nematos.rocketmq.base.AbstractMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.common.message.MessageExt;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@MQConsumer(topic = MqTopic.COP_SYNC_TO_WXCP_TOPIC, consumerGroup = MqTopic.COP_SYNC_TO_WXCP_GROUP)
public class CopSyncToWxMqConsumer extends AbstractMQPushConsumer<MqSyncCopParam> {
    @Resource
    private CopContactMqConsumerHandler copContactMqConsumerHandler;

    @Resource
    private CopFriendsMqConsumerHandler copFriendsMqConsumerHandler;

    @Resource
    private CopGroupMqConsumerHandler copGroupMqConsumerHandler;

    @Resource
    private CopMessageConsumerHandler copMessageConsumerHandler;

    @Resource
    private CopKfMqConsumerHandler copKfMqConsumerHandler;

    @Override
    public ConsumeConcurrentlyStatus dealMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        for (MessageExt messageExt : list) {
            MqSyncCopParam mqParam = parseMessage(messageExt);
            // parse ext properties
            Map<String, Object> ext = parseExtParam(messageExt);
            if (null != mqParam) {
                CopSyncToWxMqConsumer mqConsumer = SpringBeanUtil.getBean(CopSyncToWxMqConsumer.class);
                boolean result = mqConsumer.process(mqParam, ext);
                if(!result) {
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }else{
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            }
        }
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }

    @Override
    public boolean process(MqSyncCopParam mqParam, Map<String, Object> extMap) {
        String operType = mqParam.getOperType();
        //同步部门列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_DEPARTMENT_LIST.getCode())){
            return copContactMqConsumerHandler.syncDepartmentListFromWxcp(mqParam, extMap);
        }
        //同步部门员工列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_EMPLOYEE_LIST.getCode())){
            return copContactMqConsumerHandler.syncEmployeeListFromWxcp(mqParam, extMap);
        }
        //同步客户列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_FRIENDS_LIST.getCode())){
            return copFriendsMqConsumerHandler.syncFriendsFromWxcp(mqParam, extMap);
        }
        //同步客户标签列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_FRIENDS_TAGS_LIST.getCode())){
            return copFriendsMqConsumerHandler.syncFriendsTagsFromWxcp(mqParam, extMap);
        }
        //同步客户标签列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_FRIENDS_TAGS_EDIT.getCode())){
            return copFriendsMqConsumerHandler.addAndUpdateFriendsTags(mqParam, extMap);
        }
        //同步客户标签列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_FRIENDS_TAGS_DEL.getCode())){
            return copFriendsMqConsumerHandler.delFriendsTags(mqParam, extMap);
        }
        //同步客户群列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_GROUPS_LIST.getCode())){
            return copGroupMqConsumerHandler.syncGrouplistFromWxcp(mqParam, extMap);
        }
        //同步增加/修改敏感词列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_SENSITIVE_WORD_EDIT.getCode())){
            return copMessageConsumerHandler.addAndUpdateSensitiveWord(mqParam, extMap);
        }
        //同步删除敏感词列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_SENSITIVE_WORD_DEL.getCode())){
            return copMessageConsumerHandler.delSensitiveWord(mqParam, extMap);
        }
        //同步增加/修改入群欢迎语列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_GROUPS_WELCOME_EDIT.getCode())){
            return copGroupMqConsumerHandler.addAndUpdateGroupWelcome(mqParam, extMap);
        }
        //同步删除入群欢迎语列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_GROUPS_WELCOME_DEL.getCode())){
            return copGroupMqConsumerHandler.delGroupWelcome(mqParam, extMap);
        }
        //同步增加/修改入群活码列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_GROUP_ADD_JOIN_WAY.getCode())
                || operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_GROUP_UPDATE_JOIN_WAY.getCode())
                || operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_GROUP_DEL_JOIN_WAY.getCode())) {
            return copGroupMqConsumerHandler.syncGroupAddJoinWay(mqParam, extMap);
        }
        //同步向客户群群发消息任务
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_SEND_GROUP_MASS_MESSAGE.getCode())){
            return copMessageConsumerHandler.sendGroupMassMessageToWxcp(mqParam, extMap);
        }
        //同步取消向客户群群发消息任务
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_STOP_GROUP_MASS_MESSAGE.getCode())){
            return copMessageConsumerHandler.cancelGroupMassMessageToWxcp(mqParam, extMap);
        }
        //同步向客户群发消息任务
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_SEND_MASS_MESSAGE.getCode())){
            return copMessageConsumerHandler.sendMassMessageToWxcp(mqParam, extMap);
        }
        //同步取消向客户群发消息任务
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_STOP_MASS_MESSAGE.getCode())){
            return copMessageConsumerHandler.cancelMassMessageToWxcp(mqParam, extMap);
        }
        //同步群发消息列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_MESSAGE_SENDED_RECORD.getCode())){
            return copMessageConsumerHandler.syncMessageSendRecordFromWxCp(mqParam, extMap);
        }
        //发朋友圈任务
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_SEND_FANS_MOMENT.getCode())){
            return copMessageConsumerHandler.sendFansMomentToWxCp(mqParam, extMap);
        }
        //取消发朋友圈任务
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_STOP_FANS_MOMENT.getCode())){
            return copMessageConsumerHandler.cancelFansMomentToWxCp(mqParam, extMap);
        }
        //发朋友圈发布记录
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_FANS_MOMENT_PUBLISH_RECORD.getCode())){
            return copMessageConsumerHandler.syncFansMomentTaskRecordFromWxCp(mqParam, extMap);
        }
        //获取客服账号列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_KF_ACCOUNT_LIST.getCode())){
            return copKfMqConsumerHandler.syncKfAccountFromWxcp(mqParam, extMap);
        }
        //获取客服接待列表
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_KF_SERVICER_LIST.getCode())){
            return copKfMqConsumerHandler.syncKfServicerFromWxcp(mqParam, extMap);
        }
        //获取客服接待互动消息
        if(operType.equalsIgnoreCase(MqSyncOperatorTypeEnum.COP_SYNC_KF_FRIENDS_MESSAGE.getCode())){
            return copKfMqConsumerHandler.syncKfFriendsMessageFromWxcp(mqParam, extMap);
        }

        return false;
    }

}

